package com.xiaoliang.service.impl;

import com.xiaoliang.dao.ProductDao;
import com.xiaoliang.dao.impl.ProductDaoImpl;
import com.xiaoliang.entry.Product;
import com.xiaoliang.service.ProductService;
import com.xiaoliang.tools.JDBCUtils.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author XIAOLIANG
 * @ClassName ProductServiceImpl.java
 * @Description TODO
 * @createTime 2021年02月25日 20:51:00
 */
public class ProductServiceImpl implements ProductService {

	ProductDao productDao = new ProductDaoImpl();

	@Override
	public boolean addProduct(Product product) {
		Connection conn = JDBCUtils.getConnection();
		int i;
		try {
			conn.setAutoCommit(false);
			i = productDao.insert(product);
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JDBCUtils.close(conn,null);
		}
		return i>0;


	}

	@Override
	public boolean deleteProduct(int id) {
		Connection conn = JDBCUtils.getConnection();
		int i;
		try {
			conn.setAutoCommit(false);
			i = productDao.deleteByID(id);
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JDBCUtils.close(conn,null);
		}
		return i>0;

	}

	@Override
	public boolean updateProduct(Product product) {
		Connection conn = JDBCUtils.getConnection();
		int i;
		try {
			conn.setAutoCommit(false);
			i = productDao.update(product);
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JDBCUtils.close(conn,null);
		}
		return i>0;

	}

	@Override
	public Product selectOneById(int id) {
		Connection conn = JDBCUtils.getConnection();
		Product product;
		try {
			conn.setAutoCommit(false);
			product = productDao.selectById(id);
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JDBCUtils.close(conn,null);
		}
		return product;

	}

	@Override
	public List<Product> selectAll() {
		Connection conn = JDBCUtils.getConnection();
		List<Product> products;
		try {
			conn.setAutoCommit(false);
			products = productDao.selectAll();
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JDBCUtils.close(conn,null);
		}
		return products;
	}

	@Override
	public List<Product> selectPage(Integer pageNum, Integer pageSize) {
		Connection conn = JDBCUtils.getConnection();
		List<Product> products;
		try {
			conn.setAutoCommit(false);
			products = productDao.selectPage(pageNum,pageSize);
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JDBCUtils.close(conn,null);
		}
		return products;
	}

	@Override
	public List<Product> searchByCondition(String productName, String operation, String number, Integer pageNum, Integer pageSize) {

		//处理传入的数据
		if (productName==null){
			productName = "%%";
		}else {
			productName = "%"+productName+"%";
		}

		if (number==null||operation==null||"0".equals(operation)||number.isEmpty()||operation.isEmpty()){
			operation = ">";
			number = "0";
		}

		Connection connection = null;
		List<Product> products;
		try {
			connection = JDBCUtils.getConnection();
			connection.setAutoCommit(false);
			products = productDao.selectByConditionAndPage(productName, operation, number, pageNum, pageSize);
			connection.commit();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
			throw new RuntimeException(throwables);
		} finally {
			JDBCUtils.close(connection,null);
		}
		return products;
	}

	/**
	 * 获取总条数
	 *
	 * @return
	 */
	@Override
	public Integer getCount() {
		Connection conn = JDBCUtils.getConnection();
		Integer count;
		try {
			conn.setAutoCommit(false);
			count = productDao.selectCount();
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}

			e.printStackTrace();
			throw new RuntimeException(e);

		}finally {
			JDBCUtils.close(conn,null);
		}
		return count;
	}

	@Override
	public Integer getCountByCondition(String productName, String operation, String number) {



		//处理传入的数据
		if (productName==null){
			productName = "%%";
		}else {
			productName = "%"+productName+"%";
		}

		if (number==null||operation==null||"0".equals(operation)||number.isEmpty()||operation.isEmpty()){
			operation = ">";
			number = "0";
		}


		Connection connection = null;
		Integer count = null;
		try {
			connection = JDBCUtils.getConnection();
			connection.setAutoCommit(false);
			count = productDao.selectCountByCondition(productName, operation, number);
			connection.commit();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
				throw 	new RuntimeException(throwables);
		} finally {
			JDBCUtils.close(connection,null);
		}
		System.out.println(count);

		return count;
	}



}
